//
//  CYZhuDanSettingVC.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/8.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYZhuDanSettingVC.h"
#import "CYMoneyUnitSeg.h"
#import "CYShopCartBottomBtn.h"
@interface CYZhuDanSettingVC()<UITextFieldDelegate>{
    ZhuDanBlock _myblock;
    NSURLSessionTask *_task;
}
@property (nonatomic, strong) CYLabel *titleLBL;//注单设定
@property (nonatomic, strong) CYView *topLine;
@property (nonatomic, strong) CYView *midLine;
@property (nonatomic, strong) CYView *bottomLine;
@property (nonatomic, strong) CYLabel *oddsLBL;//赔率
@property (nonatomic, strong) CYLabel *rebateLBL;//返利
@property (nonatomic, strong) UISlider *slider;//滑杆
@property (nonatomic, strong) CYImageView *sliderBackImageView;//滑杆背景图
@property (nonatomic, strong) CYLabel *moneyTitle_perLBL;//单注金额 title
@property (nonatomic, strong) UITextField *money_per;//单注金额
@property (nonatomic, strong) CYMoneyUnitSeg *seg;//元角分选择控件
@property (nonatomic, strong) CYLabel *betting_times_TitleLBL;//投注倍数 title
@property (nonatomic, strong) CYShopCartBottomBtn *betting_timesView;//投注倍数加减控件
@property (nonatomic, strong) CYLabel *totalMoneyLBL;//总额
@property (nonatomic, strong) CYLabel *betting_numbersLBL;//投注注数
@property (nonatomic, strong) CYLabel *betting_timesLBL;//投注倍数
@property (nonatomic, strong) CYLabel *tipsLBL;//提示  ／／最高奖金
@property (nonatomic, strong) CYButton *enterBtn;//确认下注

@property (nonatomic, assign) CGFloat odds;//赔率
@property (nonatomic, assign) CGFloat minOdds;//最小返利
@property (nonatomic, assign) CGFloat maxOdds;//最大返利
@property (nonatomic, assign) float rebate;//返利
@property (nonatomic, assign) int times;//倍数
@property (nonatomic, assign) float totalMoney;//投注的总金额
@property (nonatomic, assign) float jiangjin;//奖金

@property (nonatomic, strong) NSDictionary *para;//请求体
@end
@implementation CYZhuDanSettingVC
- (instancetype)initBlock:(ZhuDanBlock)block{
    self = [super init];
    if (self) {
        CGFloat height = AL(PT(86)) + AL(PT(142 + 46 * 2 + 30) + AL(PT(160)) + AL(PT(98)) + AL(PT(20 * 2)) + AL(PT(160)));// 最后一个值，可以调节整个视图的高度，其他的值都是子控件的高度
        _times = 1;
        self.bounds = CGRectMake(0, 0, SCREEN_WIDTH, height);
        self.backgroundColor = [UIColor whiteColor];
        _myblock = block;
        self.titleLBL.hidden = self.topLine.hidden = self.oddsLBL.hidden = self.rebateLBL.hidden = self.sliderBackImageView.hidden = self.slider.hidden = NO;
        
        self.enterBtn.hidden = self.tipsLBL.hidden =  self.betting_timesLBL.hidden = self.betting_numbersLBL.hidden = self.totalMoneyLBL.hidden = self.bottomLine.hidden = NO;
        
        self.betting_times_TitleLBL.hidden = self.betting_timesView.hidden = self.midLine.hidden = self.moneyTitle_perLBL.hidden = self.moneyTitle_perLBL.hidden = self.seg.hidden = self.money_per.hidden = NO;
        
        
        //       self.moneyTitle_perLBL.hidden = self.seg.hidden = self.money_per.hidden = NO;
        //        self.midLine.hidden = self.bottomLine.hidden = self.betting_times_TitleLBL.hidden = self.betting_timesView.hidden = NO;
        //        self.betting_timesLBL.hidden = self.betting_numbersLBL.hidden = self.totalMoneyLBL.hidden = self.enterBtn.hidden = self.tipsLBL.hidden = NO;
    }
    return self;
}
- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:@"注单设定" size:AL(PT(36)) textcolor:ColorHex(0x333333)];
        [self addSubview:_titleLBL];
        
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self).with.offset((AL(PT(86))-_titleLBL.bounds.size.height) / 2.0);
            make.size.mas_equalTo(_titleLBL.bounds.size);
        }];
    }
    return _titleLBL;
}

- (CYView *)topLine{
    if (!_topLine) {
        _topLine = [[CYView alloc]initWithColor:ColorHex(0xb3b2b8)];
        [self addSubview:_topLine];
        
        [_topLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(_titleLBL.mas_bottom).with.offset((AL(PT(86))-_titleLBL.bounds.size.height) / 2.0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - AL(PT(35)) * 2, 0.5));
        }];
    }
    return _topLine;
}

- (CYLabel *)oddsLBL{
    if (!_oddsLBL) {
        _oddsLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"赔率:%f",_odds] size:AL(PT(26)) textcolor:ColorHex(0x333333)];
        [self addSubview:_oddsLBL];
        
        [_oddsLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(AL(PT(45)));
            make.top.equalTo(_topLine.mas_bottom).with.offset(AL(PT(46)));
            make.size.mas_equalTo(_oddsLBL.bounds.size);
        }];
    }
    return _oddsLBL;
}

- (CYLabel *)rebateLBL{
    if (!_rebateLBL) {
        _rebateLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"返利:%.3f%%",_rebate] size:AL(PT(26)) textcolor:ColorHex(0x333333)];
        [self addSubview:_rebateLBL];
        
        [_rebateLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset(- AL(PT(45)));
            make.top.equalTo(_oddsLBL);
            make.size.mas_equalTo(_rebateLBL.bounds.size);
        }];
    }
    return _rebateLBL;
}

- (CYImageView *)sliderBackImageView{
    if (!_sliderBackImageView) {
        _sliderBackImageView = [[CYImageView alloc]initWithImageName:@"sliderBackImage" size:CGSizeMake(SCREEN_WIDTH - AL(PT(45)) * 2, AL(PT(20))) isRadius:NO];
        [self addSubview:_sliderBackImageView];
        
        [_sliderBackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(_oddsLBL.mas_bottom).with.offset(AL(PT(46)));
            make.size.mas_equalTo(_sliderBackImageView.bounds.size);
        }];
    }
    return _sliderBackImageView;
}

- (UISlider *)slider{
    if (!_slider) {
        _slider = [[UISlider alloc]init];
        _slider.maximumTrackTintColor = RGBA(234, 218, 209, 1);
        _slider.minimumTrackTintColor = RGBA(250, 92, 33, 1);
        [_slider setThumbImage:[UIImage imageNamed:@"sliderThumbImage"] forState:UIControlStateNormal];//设置滚动小圆点图片
        [_slider addTarget:self action:@selector(actionForSlider:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_slider];
        
        [_slider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_sliderBackImageView);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - AL(PT(55)) * 2, AL(PT(30))));
        }];
        
    }
    return _slider;
}

- (CYLabel *)moneyTitle_perLBL{
    if (!_moneyTitle_perLBL) {
        _moneyTitle_perLBL = [[CYLabel alloc]initWithText:@"单注金额" size:AL(PT(26)) textcolor:ColorHex(0x888888)];
        [self addSubview:_moneyTitle_perLBL];
        
        [_moneyTitle_perLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(AL(PT(45)));
            make.bottom.equalTo(_midLine.mas_top).with.offset( - (AL(PT(142)) - _moneyTitle_perLBL.bounds.size.height) / 2.0);
            make.size.mas_equalTo(_moneyTitle_perLBL.bounds.size);
            
        }];
    }
    return _moneyTitle_perLBL;
}

- (CYMoneyUnitSeg *)seg{
    if (!_seg) {
        WS(self)
        _seg = [[CYMoneyUnitSeg alloc]initWithColor:[UIColor whiteColor] size:CGSizeMake(AL(PT(240)), AL(PT(60))) block:^(NSString *string) {
            [weakSelf moneyUnit:string];
        }];
        [self addSubview:_seg];
        [_seg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).with.offset(-AL(PT(45)));
            make.centerY.equalTo(_moneyTitle_perLBL);
            make.size.mas_equalTo(_seg.bounds.size);
        }];
    }
    return _seg;
}

- (UITextField *)money_per{
    if (!_money_per) {
        _money_per = [[UITextField alloc]init];
        _money_per.text = @"2";
        _money_per.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        _money_per.textAlignment = NSTextAlignmentCenter;
        _money_per.borderStyle = UITextBorderStyleRoundedRect;
        _money_per.keyboardType = UIKeyboardTypeNumberPad;
        _money_per.font = [UIFont systemFontOfSize:AL(PT(26))];
        [_money_per addTarget:self action:@selector(money_perDidChanged:) forControlEvents:UIControlEventEditingChanged];
        _money_per.delegate = self;
        [self addSubview:_money_per];
        
        [_money_per mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_seg.mas_left).with.offset(- AL(PT(14)));
            make.centerY.equalTo(_moneyTitle_perLBL);
            make.size.mas_equalTo(CGSizeMake(AL(PT(143)), AL(PT(60))));
        }];
    }
    return _money_per;
}

- (CYView *)midLine{
    if (!_midLine) {
        _midLine = [[CYView alloc]initWithColor:ColorHex(0xb3b2b8)];
        [self addSubview:_midLine];
        
        [_midLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(_betting_times_TitleLBL.mas_top).with.offset(-(AL(PT(160)) - _betting_times_TitleLBL.bounds.size.height) / 2.0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - AL(PT(35)) * 2, 0.5));
        }];
    }
    return _midLine;
}

- (CYLabel *)betting_times_TitleLBL{
    if (!_betting_times_TitleLBL) {
        _betting_times_TitleLBL = [[CYLabel alloc]initWithText:@"投注倍数" size:AL(PT(26)) textcolor:ColorHex(0x888888)];
        [self addSubview:_betting_times_TitleLBL];
        
        [_betting_times_TitleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_bottomLine.mas_top).with.offset(- (AL(PT(160)) - _betting_times_TitleLBL.bounds.size.height) / 2.0);
            make.left.equalTo(self).with.offset(AL(PT(45)));
            make.size.mas_equalTo(_betting_times_TitleLBL.bounds.size);
        }];
    }
    return _betting_times_TitleLBL;
}

- (CYShopCartBottomBtn *)betting_timesView{
    if (!_betting_timesView) {
        WS(self)
        _betting_timesView = [[CYShopCartBottomBtn alloc]initWithColor:[UIColor whiteColor] block:^(int times) {
            weakSelf.times = ((weakSelf.times > 1 && times < 0) || times > 0)?weakSelf.times + times:weakSelf.times;
        }];
        [self addSubview:_betting_timesView];
        
        [_betting_timesView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_betting_times_TitleLBL);
            make.right.equalTo(self).with.offset(- AL(PT(45)));
            make.size.mas_equalTo(CGSizeMake(AL(PT(394)), AL(PT(60)) ) );
        }];
    }
    return _betting_timesView;
}

- (CYView *)bottomLine{
    if (!_bottomLine) {
        _bottomLine = [[CYView alloc]initWithColor:ColorHex(0xb3b2b8)];
        [self addSubview:_bottomLine];
        
        [_bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_betting_timesLBL.mas_top).with.offset(-10);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - AL(PT(35)) * 2, 0.5));
            make.centerX.equalTo(self);
        }];
    }
    return _bottomLine;
}

- (CYLabel *)betting_timesLBL{
    if (!_betting_timesLBL) {
        _betting_timesLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"倍数:%d注",_times] size:AL(PT(26)) textcolor:ColorHex(0x888888)];
        [self addSubview:_betting_timesLBL];
        
        [_betting_timesLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset( - AL(PT(45)));
            make.bottom.equalTo(_tipsLBL.mas_top).with.offset(-AL(30));
            make.size.mas_equalTo(_betting_timesLBL.bounds.size);
        }];
    }
    return _betting_timesLBL;
}

- (CYLabel *)betting_numbersLBL{
    if (!_betting_numbersLBL) {
        _betting_numbersLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"注数:%lu注",_number] size:AL(PT(26)) textcolor:ColorHex(0x888888)];
        [self addSubview:_betting_numbersLBL];
        
        [_betting_numbersLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_betting_timesLBL.mas_left).with.offset( - AL(PT(10)));
            make.top.equalTo(_betting_timesLBL);
            make.size.mas_equalTo(_betting_numbersLBL.bounds.size);
        }];
    }
    return _betting_numbersLBL;
}

- (CYLabel *)totalMoneyLBL{
    if (!_totalMoneyLBL) {
        _totalMoneyLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"总额:%lu元",(unsigned long)_totalMoney] size:AL(PT(26)) textcolor:ColorHex(0x888888)];
        [self addSubview:_totalMoneyLBL];
        
        [_totalMoneyLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_betting_numbersLBL.mas_left).with.offset( - AL(PT(10)));
            make.top.equalTo(_betting_timesLBL);
            make.size.mas_equalTo(_totalMoneyLBL.bounds.size);
        }];
    }
    return _totalMoneyLBL;
}

- (CYButton *)enterBtn{
    if (!_enterBtn) {
        _enterBtn = [[CYButton alloc]initWithTitle:@"确认下注" titleColor:[UIColor whiteColor] backgroundColor:THEME_COLOR cornerRadius:0.0];
        [_enterBtn setTitleColor:ColorHex(0xb3b2b8) forState:UIControlStateHighlighted];
        [_enterBtn addTarget:self action:@selector(actionForEnterBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_enterBtn];
        
        [_enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self.mas_bottom).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, AL(PT(98))));
        }];
    }
    return _enterBtn;
}
- (CYLabel *)tipsLBL{
    if (!_tipsLBL) {
        _tipsLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"若中奖，奖金最高可达%.3f元",_jiangjin] size:AL(PT(28)) textcolor:ColorHex(0x333333)];
        [self addSubview:_tipsLBL];
        
        [_tipsLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(_enterBtn.mas_top).with.offset(- AL(PT(20)));
            make.size.mas_equalTo(CGSizeMake(_tipsLBL.bounds.size.width, ceil(_tipsLBL.bounds.size.height)));
        }];
    }
    return _tipsLBL;
}

//判断是否是快速下注还是添加注单，如果是快速下注，更改下注按钮的title
- (void)setisBet:(BOOL)isBet{
    if (_isBet != isBet) {
        _isBet = isBet;
        if (!isBet)
            [_enterBtn setTitle:@"添加注单" forState:UIControlStateNormal];
    }
}

- (void)setModel:(CYCurrentBetModel *)model{
    if (_model != model) {
        _model = model;
        _slider.minimumValue = 0;//下限
        _slider.maximumValue = model.fanDianMax;//上限
        self.odds = model.bonusProp;//当前显示的赔率
        self.maxOdds = model.bonusProp;//最大赔率
        self.minOdds = model.bonusPropBase;//最小赔率
        self.jiangjin = self.maxOdds * [_money_per.text integerValue];//奖金
    }
    
}

- (void)setActionData:(NSString *)actionData{
    if (_actionData != actionData) {
        _actionData = actionData;
        if([_model.playedTpl isEqualToString:@"ssc_ts_h3"] || [_model.playedTpl isEqualToString:@"k3_hz"]){//时时彩-三星-后三特殊号码
            self.oddsLBL.hidden = YES;
            [self special:_actionData];
        }
        else{
            self.oddsLBL.hidden = NO;
            [self clear];
            self.odds = _model.bonusProp;//当前显示的赔率
            self.maxOdds = _model.bonusProp;//最大赔率
            self.minOdds = _model.bonusPropBase;//最小赔率
            self.jiangjin = self.maxOdds * [_money_per.text integerValue];//奖金
        }
    }
    
}
//赔率
- (void)setOdds:(CGFloat )odds{
    if (_odds != odds)
        _odds = odds;
    _oddsLBL.text = [NSString stringWithFormat:@"赔率:%.3f",_odds];
    [_oddsLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_oddsLBL.bounds.size);
    }];
    if (_oddsLBL.hidden == NO)
        self.jiangjin = _odds * _totalMoney;//赔率 * 投注的总金额
    
}

//返利
- (void)setRebate:(float)rebate{
    if (_rebate != rebate)
        _rebate = rebate;
    _rebateLBL.text = [NSString stringWithFormat:@"返利:%.3f%%",_rebate];
    [_rebateLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_rebateLBL.bounds.size);
    }];
}

//倍数
- (void)setTimes:(int)times{
    if (_times != times)
        _times = times;
    self.betting_timesLBL.text = [NSString stringWithFormat:@"倍数:%d倍",_times];
    [_betting_timesLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_betting_timesLBL.bounds.size);
    }];
    self.totalMoney = [_money_per.text integerValue] * _number * _times;//单注金额 * 注数 * 倍数
    
}

//注数
- (void)setNumber:(NSUInteger)number{
    if (_number != number)
        _number = number;
    self.betting_numbersLBL.text = [NSString stringWithFormat:@"注数:%lu注",(unsigned long)_number];
    [_betting_numbersLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_betting_numbersLBL.bounds.size);
    }];
    self.totalMoney = [_money_per.text integerValue] * _number * _times;//单注金额 * 注数 * 倍数
    
}

//投注的总金额
- (void)setTotalMoney:(float)totalMoney{
    if (_totalMoney != totalMoney)
        _totalMoney = totalMoney;
    [self setupStringColorOfTotalMoneyLBL];
    //    self.jiangjin = _odds * _totalMoney;//提示最高奖金 = 赔率 * 单注金额
}

//奖金
- (void)setJiangjin:(float)jiangjin{
    if (_jiangjin != jiangjin )
        _jiangjin = jiangjin;
    [self setupStringColorOfTipsLBL];
}

//设置总额，数字部分显示红色
- (void)setupStringColorOfTotalMoneyLBL{
    
    //    NSString *totalMoneyStr = [NSString commaBetweenThreeCharacters:[NSString stringWithFormat:@"%.2f",_totalMoney]];
    NSString *string = [NSString stringWithFormat:@"总额:%.3f元",_totalMoney];
    NSMutableAttributedString *attributedStr  = [[NSMutableAttributedString alloc]initWithString:string];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:THEME_COLOR range:NSMakeRange(3, string.length - 4)];
    _totalMoneyLBL.attributedText = attributedStr;
    [_totalMoneyLBL sizeToFit];
    [_totalMoneyLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_totalMoneyLBL.bounds.size);
    }];
    
}

//设置提示，数字部分显示红色
- (void)setupStringColorOfTipsLBL{
    //    NSString *jiangjinStr = [NSString commaBetweenThreeCharacters:[NSString stringWithFormat:@"%lu",(unsigned long)_jiangjin]];
    NSString *string = [NSString stringWithFormat:@"若中奖，奖金最高可达%.3f元",_jiangjin];
    NSMutableAttributedString *attributedStr  = [[NSMutableAttributedString alloc]initWithString:string];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:THEME_COLOR range:NSMakeRange(10, string.length - 11)];
    _tipsLBL.attributedText = attributedStr;
    [_tipsLBL sizeToFit];
    [_tipsLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_tipsLBL.bounds.size);
    }];
    
}

//输入金额已经改变
- (void)money_perDidChanged:(UITextField *)textField{
    
    NSUInteger money = [textField.text integerValue];
    if (money > 9999999)
        [CYAlert showInWindowWithTitle:@"注意" message:@"单注投注金额限制100万以内" complete:^{
            textField.text = @"2";
            self.totalMoney = [_money_per.text integerValue] * _number * _times;//投注的总金额 = 单注金额 * 注数 * 倍数
        }];
    else{
        self.totalMoney = [_money_per.text integerValue] * _number * _times;//投注的总金额 = 单注金额 * 注数 * 倍数
    }
}

//滑杆
- (void)actionForSlider:(UISlider *)sender{
    self.rebate = sender.value;//返利
    self.odds = _maxOdds - (_maxOdds - _minOdds) * sender.value / _slider.maximumValue;//赔率
    if([_model.playedTpl isEqualToString:@"ssc_ts_h3"] || [_model.playedTpl isEqualToString:@"k3_hz"]){
        [self changeSliderValue:sender.value];
    }
}

//金额单位block
- (void)moneyUnit:(NSString *)string{
    DeLog(@"当前的金额单位  %@",string);
}

//确认下注和快速下注后，times和赔率清零
- (void)resert{
    self.times  = _betting_timesView.times = 1;
    _slider.value = 0.0;
    [self performSelector:@selector(actionForSlider:) withObject:_slider];
}

//确认下注
- (void)actionForEnterBtn:(CYButton *)sender{
    self.para = [self parameters];
    NSString *jsonString = _para[@"data"];
    NSDictionary *zhudanDic = [NSDictionary dictWithJson:jsonString];
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        if (!_isBet)//如果是添加注单
            _myblock(zhudanDic,NO);
        else{//快速下单
            _myblock(nil, NO);
            [self https];
        }
    }];
    [self resert];
}

//投注请求
- (void)https{
    
    _task = [CYHttps POST:API_bet parameters:_para showHUD:YES progress:nil success:^(id data) {
        DeLog(@"投注data %@",data);
        if ([data isKindOfClass:[NSString class]] || [data isKindOfClass:[NSNull class]] || data)
            [CYSuccessTipsView showTitle:@"投注成功!" buttonTitle:@"确定" block:^{
                _myblock(nil, YES);
            }];
    } failure:nil];
    
}

- (NSDictionary *)parameters{
    if([_model.playedTpl isEqualToString:@"ssc_ts_h3"] || [_model.playedTpl isEqualToString:@"k3_hz"])
        return [self anotherPara];
    else{
        NSArray *array = @[@{@"actionData":_actionData,//投注号码
                             @"fanDian":@(_slider.value / _slider.maximumValue),//返利
                             @"beiShu":@(_times),//倍数
                             @"actionNum":@(_number),//注数
                             @"actionPrice":@(_totalMoney / _times),//投注总金额(除以倍数，后台会自动乘以倍数)
                             @"onePrice":@([_money_per.text integerValue]),//单注金额
                             @"playedId":@(_model.secondBetID),//玩法id
                             @"playedGroup":@(_model.firstBetID),//玩法组id
                             @"bonusProp":[NSString stringWithFormat:@"%.3f",_odds],//赔率
                             @"mode":@(2)}];//数2元0.2角0.02分
        
        NSMutableDictionary *para = [NSMutableDictionary dictionary];
        [para setValue:[CYUser shareUser].token forKey:@"token"];//token
        [para setValue:[CYUser shareUser].username forKey:@"username"];//username
        [para setValue:@(_model.lotteryID) forKey:@"id"];//彩种id
        [para setValue:_model.actionNo forKey:@"action_No"];//期号
        [para setValue:array forKey:@"code"];//投注信息
        return @{@"data":[NSDictionary jsonWithDict:para]};
    }
    return nil;
}

- (NSDictionary *)anotherPara{
    NSMutableDictionary *para = [NSMutableDictionary dictionary];
    [para setValue:[CYUser shareUser].token forKey:@"token"];//token
    [para setValue:[CYUser shareUser].username forKey:@"username"];//username
    [para setValue:@(_model.lotteryID) forKey:@"id"];//彩种id
    [para setValue:_model.actionNo forKey:@"action_No"];//期号
    
    NSArray *array = [_actionData componentsSeparatedByString:@","];
    
    NSMutableArray *mutableArr = [NSMutableArray array];
    
    
    CYLeastModel *subModel;
    for (int i = 0; i < array.count; i++) {
        if ([array[i] isEqualToString:@"豹子"])
            subModel = _model.gatherArr[0];
        else if ([array[i] isEqualToString:@"顺子"])
            subModel = _model.gatherArr[1];
        else if ([array[i] isEqualToString:@"对子"])
            subModel = _model.gatherArr[2];
        else
            subModel = _model.gatherArr[[array[i] integerValue] - 3];
        
        float peilv = subModel.bonusProp - (subModel.bonusProp - subModel.bonusPropBase) * _slider.value / _slider.maximumValue;//赔率
        NSString *peilvStr = [NSString  stringWithFormat:@"%.3f",peilv];
        NSDictionary *dict = @{@"actionData":array[i],//投注号码
                               @"fanDian":@(_slider.value / _slider.maximumValue),//返利
                               @"beiShu":@(_times),//倍数
                               @"actionNum":@(1),//注数
                               @"actionPrice":@(2),//投注总金额(除以倍数，后台会自动乘以倍数)
                               @"onePrice":@([_money_per.text integerValue]),//单注金额
                               @"playedId":@(_model.secondBetID),//玩法id
                               @"playedGroup":@(_model.firstBetID),//玩法组id
                               @"bonusProp":peilvStr,//赔率
                               @"mode":@(2)};//数2元0.2角0.02分
        [mutableArr addObject:dict];
        [para setValue:mutableArr forKey:@"code"];//投注信息
        
    }
    return @{@"data":[NSDictionary jsonWithDict:para]};
    
}
//清除
- (void)clear{
    
    for (int i = 0; i < 16; i++) {
        CYLabel *label = [self viewWithTag:400 + i];
        if (label)
            [label removeFromSuperview];
    }
    
}
- (void)special:(NSString *)actionData{
    [self clear];
    
    NSArray *array = [actionData componentsSeparatedByString:@","];
    
    CGSize size = [@"万" sizeWithAttributes:@{NSFontAttributeName :[UIFont systemFontOfSize:AL(13)]}];
    NSUInteger totalRow = ceil(array.count / 3.0);//行数
    CGFloat totalH = totalRow * ceil(size.height);//总高度
    
    CGFloat offset = ([_model.playedTpl isEqualToString:@"k3_hz"])?(_sliderBackImageView.bounds.size.width - 45)/4.0:(_sliderBackImageView.bounds.size.width - 40)/3.0;//距离左边label的距离
    CGFloat jiangjin = 0;
    _oddsLBL.hidden = YES;
    CYLeastModel *subModel;
    
    [self setNeedsDisplay];
    for (int i = 0; i < array.count; i++) {
        NSUInteger row = i / 4;//行数
        NSUInteger column = i % 4;
        if ([array[i] isEqualToString:@"豹子"])
            subModel = _model.gatherArr[0];
        else if ([array[i] isEqualToString:@"顺子"])
            subModel = _model.gatherArr[1];
        else if ([array[i] isEqualToString:@"对子"])
            subModel = _model.gatherArr[2];
        else
            subModel = _model.gatherArr[[array[i] integerValue] - 3];
        
        
        CYLabel *label = [[CYLabel alloc]init];
        label.tag = 400 + i;
        label.font = [UIFont systemFontOfSize:13];
        NSString *string = [NSString stringWithFormat:@"%@:%.3f",subModel.number, subModel.bonusProp];
        NSMutableAttributedString *attributedStr  = [[NSMutableAttributedString alloc]initWithString:string];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:THEME_COLOR range:NSMakeRange(0, 2)];
        label.attributedText = attributedStr;
        [label sizeToFit];
        [self addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            if ([_model.playedTpl isEqualToString:@"k3_hz"])
                make.left.equalTo(_sliderBackImageView).with.offset((offset + 15) * column);
            else
                make.left.equalTo(_sliderBackImageView).with.offset((offset + 20) * i);
            make.size.mas_equalTo(label.bounds.size);
            make.top.equalTo(_sliderBackImageView.mas_bottom).with.offset(5 + size.height * row);
        }];
//        offset += label.bounds.size.width;
        jiangjin += subModel.bonusProp * 2;
        //        DeLog(@"名称  %@ 赔率  %f  最低赔率  %f",subModel.number,subModel.bonusProp,subModel.bonusPropBase);
    }
    self.jiangjin = jiangjin;
    
}
- (void)changeSliderValue:(float)value{
    NSArray *array = [_actionData componentsSeparatedByString:@","];
    DeLog(@"滑杆  %f",value);
    CYLeastModel *subModel;
    CGFloat jiangjin = 0;
    for (int i = 0; i < array.count; i++) {
        CYLabel *label = [self viewWithTag:400 + i];
        if ([array[i] isEqualToString:@"豹子"])
            subModel = _model.gatherArr[0];
        else if ([array[i] isEqualToString:@"顺子"])
            subModel = _model.gatherArr[1];
        else if ([array[i] isEqualToString:@"对子"])
            subModel = _model.gatherArr[2];
        else
            subModel = _model.gatherArr[[array[i] integerValue] - 3];
        
        float peilv = subModel.bonusProp - (subModel.bonusProp - subModel.bonusPropBase) * value / _slider.maximumValue;//赔率
        NSString *string = [NSString stringWithFormat:@"%@:%.3f",subModel.number,peilv];
        NSMutableAttributedString *attributedStr  = [[NSMutableAttributedString alloc]initWithString:string];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:THEME_COLOR range:NSMakeRange(0, 2)];
        label.attributedText = attributedStr;
        [label sizeToFit];
        
        [label mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(label.bounds.size);
        }];
        jiangjin +=peilv * 2;
    }
    self.jiangjin = jiangjin;
}

- (void)dealloc{
    [_task cancel];
    _task = nil;
    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
